﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
namespace syc.Sale
{
    public partial class HeTong_YuanLiao : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {  
            int BillID = 0;
            if (!this.IsPostBack)
            {
                this.DZList.DataBind();
                this.GJList.DataBind();
                this.GGList.DataBind();
                this.GongHuo.DataBind();
                this.YuanLiao.DataBind();
                this.JingBan.DataBind();
                this.ZhuGuan.DataBind();
                this.FaHuo.DataBind();
                GongHuo_SelectedIndexChanged(null, null);
                YuanLiao_SelectedIndexChanged(null, null);


                if (Request.QueryString["ID"] != null)
                {
                    int.TryParse(Request.QueryString["ID"], out BillID);
                }
                if (BillID > 0)
                {
                    SqlConnection Conn = new SqlConnection();
                    Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
                    Conn.Open();
                    SqlCommand Cmd = new SqlCommand();
                    Cmd.Connection = Conn;
                    Cmd.CommandText = "select [供货单位ID],  [原料ID], [合同编号], [数量], [运输方式], [经办人], [部门主管], [备注], [日期],[金额] from [原料采购合同] where [ID]=" + BillID;
                    Cmd.CommandType = CommandType.Text;
                    SqlDataReader Reader = Cmd.ExecuteReader();
                    if (Reader.Read())
                    {
                        this.ViewState["BillID"] = BillID;
                        this.RiQi.Text = Reader.GetDateTime(8).ToString("yyyy年M月d日");
                        this.HeTongHao.Text = Reader.GetString(2).Trim();
                        this.HeTongHao.ReadOnly = true;
                        this.HeTongHao.CssClass = "blank_left";
                        this.CaiGouLiang.Text = Reader.GetFloat(3).ToString();
                        this.BeiZhu.Text = Reader.GetString(7).Trim();                       
                        this.Jine.Text = Reader.GetDecimal(9).ToString("c");                     
                        string t;
                        t = Reader.GetInt32(0).ToString();
                        if (this.GongHuo.Items.FindByValue(t) != null)
                        {
                            this.GongHuo.Text = t;
                            this.GongHuoDiZhi.Text = this.DZList.Items.FindByValue(t).Text;
                        }


                        t = Reader.GetInt32(1).ToString();
                        if (this.YuanLiao.Items.FindByValue(t) != null)
                        {
                            this.YuanLiao.Text = t;
                            this.GangJi.Text = this.GJList.Items.FindByValue(t).Text;
                            this.GuiGe.Text = this.GGList.Items.FindByValue(t).Text;

                        }
                        t = Reader.GetString(4).Trim();
                        if (this.FaHuo.Items.FindByValue(t) != null)
                        {
                            this.FaHuo.Text = t;
                        }
                        t = Reader.GetString(5).Trim();
                        if (this.JingBan.Items.FindByValue(t) != null)
                        {
                            this.JingBan.Text = t;
                        }
                        t = Reader.GetString(6).Trim();
                        if (this.ZhuGuan.Items.FindByValue(t) != null)
                        {
                            this.ZhuGuan.Text = t;
                        }
                        Reader.Close();
                    }
                    else
                    {
                        this.RiQi.Text = DateTime.Today.ToString("yyyy年M月d日");
                        Reader.Close();
                    }

                    Conn.Close();
                    Reader.Dispose();
                    Cmd.Dispose();
                    Conn.Dispose();
                }
                else
                {
                    this.RiQi.Text = DateTime.Today.ToString("yyyy年M月d日");
                }
            }
            this.Message.Text = "·准备就绪";
        }

        protected void GongHuo_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.GongHuoDiZhi.Text = DZList.Items.FindByValue(this.GongHuo.Text).Text;
        }


        protected void YuanLiao_SelectedIndexChanged(object sender, EventArgs e)
        {
            string id = this.YuanLiao.Text;
            this.GangJi.Text = this.GJList.Items.FindByValue(id).Text;
            this.GuiGe.Text = this.GGList.Items.FindByValue(id).Text;
        }

        protected void Button_Finish_Click(object sender, EventArgs e)
        {
            DateTime dt;
            float dhl;
            Decimal jine;
            if (!DateTime.TryParse(this.RiQi.Text, out dt))
            {
                this.Message.Text = "·日期输入有误";
                return;
            }
            if (!float.TryParse(this.CaiGouLiang.Text, out dhl))
            {
                this.Message.Text = "·订货量输入有误";
                return;
            }
            if (!Decimal.TryParse(this.Jine.Text, out jine))
            {
                this.Message.Text = "·订货金额输入有误";
                return;
            }
            if (this.HeTongHao.Text.Trim().Length == 0)
            {
                this.Message.Text = "·合同号必须录入";
                return;
            }
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlTransaction Tran = Conn.BeginTransaction();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            Cmd.Transaction = Tran;
            try
            {
                Cmd.CommandText = "HeTong_YuanLiao";
                Cmd.CommandType = CommandType.StoredProcedure;
                if (this.ViewState["BillID"] == null)
                {
                    this.ViewState["BillID"] = -1;
                }
                Cmd.Parameters.Add(new SqlParameter("@供货单位ID", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, int.Parse(this.GongHuo.Text)));
                Cmd.Parameters.Add(new SqlParameter("@原料ID", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, int.Parse(this.YuanLiao.Text)));
                Cmd.Parameters.Add(new SqlParameter("@合同编号", SqlDbType.Char, 20, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.HeTongHao.Text));
                Cmd.Parameters.Add(new SqlParameter("@数量", SqlDbType.Real, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, dhl));
                Cmd.Parameters.Add(new SqlParameter("@金额", SqlDbType.Money, 20, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, jine));
                Cmd.Parameters.Add(new SqlParameter("@运输方式", SqlDbType.NChar, 20, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.FaHuo.Text));
                Cmd.Parameters.Add(new SqlParameter("@经办人", SqlDbType.NChar, 20, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.JingBan.Text));
                Cmd.Parameters.Add(new SqlParameter("@部门主管", SqlDbType.NChar, 20, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.ZhuGuan.Text));
                Cmd.Parameters.Add(new SqlParameter("@备注", SqlDbType.NVarChar, 400, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.BeiZhu.Text));
                Cmd.Parameters.Add(new SqlParameter("@日期", SqlDbType.Date, 3, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, DateTime.Parse(RiQi.Text)));
                Cmd.Parameters.Add(new SqlParameter("@BillID", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.ViewState["BillID"]));
                Cmd.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int, 4, ParameterDirection.Output, true, 0, 0, "", DataRowVersion.Proposed, null));

                Cmd.ExecuteNonQuery();

                this.ViewState["BillID"] = (int)Cmd.Parameters["@ID"].Value;
                int BillID = (int)this.ViewState["BillID"];
                if (BillID > 0)
                {
                    this.HeTongHao.CssClass = "blank_left";
                    this.Message.Text = "·保存成功";
                }
                else
                {
                    this.Message.Text = "·合同编号已经存在";
                }
                Tran.Commit();
            }
            catch (Exception err)
            {
                Tran.Rollback();
                this.Message.Text = err.Message;
            }
            Cmd.Dispose();
            Tran.Dispose();
            Conn.Close();
            Conn.Dispose();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("HeTong_YuanLiao_Search.aspx");
        }
        

       
    }
}
